﻿Option Strict On
Option Explicit On
Option Infer On

Imports System
Imports System.Linq
Imports System.Linq.Expressions
Imports System.Collections.Generic
Imports GenDotNet.Task.Common

Public Class ConditionalInfo

   Private mExpression As String
   Private mContents As EmptyDelegate

   Public Sub New(ByVal expression As String, ByVal contents As EmptyDelegate)
      Me.mExpression = expression
      Me.mContents = contents
   End Sub

   Public Property Expression() As String
      Get
         Return mExpression
      End Get
      Set(ByVal value As String)
         mExpression = value
      End Set
   End Property

   Public Property Contents() As EmptyDelegate
      Get
         Return mContents
      End Get
      Set(ByVal value As EmptyDelegate)
         mContents = value
      End Set
   End Property

End Class

Namespace VB
   Public Module ConditionalInfoExtensions
      <System.Runtime.CompilerServices.Extension()> _
      Public Function Output(ByVal info As ConditionalInfo, ByVal data As Object) As String
         Return _
<code>
   If <%= info.Expression %> Then
      <%= info.Contents.Invoke() %>
   End If
</code>.Value
      End Function

      <System.Runtime.CompilerServices.Extension()> _
     Public Function Output(ByVal info As ConditionalInfo) As String
         Return info.Output(Nothing)
      End Function
   End Module
End Namespace

Namespace CSharp
   Public Module ConditionalInfoExtensions
      <System.Runtime.CompilerServices.Extension()> _
       Public Function Output(ByVal info As ConditionalInfo, ByVal data As Object) As String
         Return _
   <code>
      if (<%= FixExpression(info.Expression) %>)
      {
         <%= info.Contents.Invoke() %>
      }
</code>.Value
      End Function

      <System.Runtime.CompilerServices.Extension()> _
     Public Function Output(ByVal info As ConditionalInfo) As String
         Return info.Output(Nothing)
      End Function

      Private Function FixExpression(ByVal expression As String) As String
         ' I should probably change this to RegEx
         Return expression.Replace("<>", "!=").Replace(" Not ", " ! "). _
                  Replace("=", "==").Replace(" Is ", " == "). _
                  Replace(" IsNot ", " != ").Replace(" Nothing", " null"). _
                  Replace("!==", "!=").Replace(" And ", " & ").Replace(" Or ", " | ").Replace(" AndAlso ", " && ").Replace(" OrElse ", " || ")
      End Function

   End Module
End Namespace
